<template>
    <div id="option">
      <el-col :span="24" class="action-bar">
        <el-button type="primary" size="medium" @click="addSubject">添加选项</el-button>
      </el-col>

      <!--列表-->
      <el-table :data="optionList" highlight-current-row class="el-table-style">
        <el-table-column type="index" width="100" sortable></el-table-column>
        <el-table-column prop="answer" label="选项内容" width="500" sortable></el-table-column>
        <el-table-column prop="num" label="选项编号" width="250" sortable></el-table-column>
        <el-table-column label="正确选项" width="200" sortable>
          <template slot-scope="scope">
            <p>{{scope.row.is_right_answer==1 ? '是' : '否'}}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" sortable>
          <template slot-scope="scope">
            <el-button type="primary" class="danger-btn" size="mini" @click="questionsAnswersDetail(scope.row.id)">编辑</el-button>
            <el-button type="danger" class="danger-btn" size="mini" @click="delQuestionsAnswers(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加 -->
      <!-- 新增 -->
      <el-dialog title="添加/编辑选项" :visible.sync="addFormVisible" width="30%">
        <el-form label-width="110px" label-position="left">
          <el-form-item label="选项编号">
            <el-select v-model="addForm.num" placeholder="请选择选项编号" class="action-bar-input">
              <el-option label="A" value="A"></el-option>
              <el-option label="B" value="B"></el-option>
              <el-option label="C" value="C"></el-option>
              <el-option label="D" value="D"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="选项内容" style="height: 200px;">
            <el-input type="textarea" :rows="4" v-model="addForm.answer" placeholder="请输入选项内容"></el-input>
          </el-form-item>
          <el-form-item label="正确答案">
            <el-select v-model="addForm.is_right_answer" placeholder="是否为正确答案" class="action-bar-input">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click="setQuestionsAnswers">提交</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
    export default {
      name: "optionList",
      data(){
          return {
            optionList:[],
            addFormVisible: false,
            addForm:{}
          }
      },
      mounted(){
        this.addForm.q_id = this.$route.query.tm_id;
        this.questionsAnswersList();
      },
      methods:{
        //获取选项列表
        async questionsAnswersList(){
          let params = {
            q_id : this.$route.query.tm_id
          }
          const res = await this.$api.questionsAnswersList(params);
          if (res.data.level == 'success') {
            this.optionList = res.data.data;
          }else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        },
        //获取选项详情
        async questionsAnswersDetail(id){
          let params = {
            id : id
          }
          const res = await this.$api.questionsAnswersDetail(params);
          if (res.data.level == 'success'){
            this.addForm = res.data.data;
            this.addFormVisible = true;
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        },
        //添加选项
        async setQuestionsAnswers(){
          let params = this.addForm;
          const res = await this.$api.setQuestionsAnswers(params);
          if (res.data.level == 'success'){
            this.$message({
              message: res.data.message,
              type: 'success'
            })
            this.questionsAnswersList();
            this.addFormVisible = false;
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        },
        //删除选项
        async delQuestionsAnswers(id){
          let params = {
            id : id
          }
          const res = await this.$api.delQuestionsAnswers(params);
          if (res.data.level == 'success'){
            this.$message({
              message: res.data.message,
              type: 'success'
            })
            this.questionsAnswersList();
          } else {
            this.$message({
              message: res.data.message,
              type: 'warning'
            })
          }
        },
        //添加按钮
        addSubject(){
          this.addForm = {
            q_id : this.$route.query.tm_id
          }
          this.addFormVisible = true;
        }
      }
    }
</script>

<style scoped>
  .dialog-footer{
    text-align: center
  }
</style>
